package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by lijingyu on 2018/5/31.
 */
@RestController
public class EmployeeController {

    @Autowired
    private EmployeeRepository employeeRepository;

    @GetMapping(value = "/employees")
    public List<Employee> getEmployees() {
        return employeeRepository.findAll();
    }

    @GetMapping(value = "/employees/add")
    public Employee employeeAdd() {
        Employee employee = new Employee();
        employee.setEname("张无极001");
        employee.setSex("女");
        employee.setBirthday(new Date());
        employee.setJoinDate(new Date());
        employee.setEno("2018221001");
        employee.setUsername("zs001");
        employee.setPassword("123321");
        employee.setDno(5);
        return employeeRepository.save(employee);
    }

    @PutMapping(value = "/employees/update/{id}")
    public Employee employeeUpdate(@PathVariable("id") Integer id) {
        Employee employee = new Employee();
        employee.setEid(id);
        employee.setSex("女");
        employee.setDno(5);
        return employeeRepository.save(employee);
    }

    @GetMapping(value = "/employees/get/{id}")
    public Employee employeeFindOne(@PathVariable("id") Integer id) {
         return employeeRepository.findOne(id);
    }

    @DeleteMapping(value = "/employees/delete/{id}")
    public void employeeDelete(@PathVariable("id") Integer id) {
        employeeRepository.delete(id);
    }

    @GetMapping(value = "/employees/dno/{dno}")
    public List<Employee> employeeListByDno(@PathVariable("dno") Integer dno) {
        return employeeRepository.findByDno(dno);
    }

}
